//
//  LBMEmoticonManager.swift
//  EmoticonKeyboard
//
//  Created by 刘保满 on 16/5/30.
//  Copyright © 2016年 itheima. All rights reserved.
// 负责加载表情数据

import UIKit

class LBMEmoticonManager: NSObject {

    //单例
    static let sharedEmoticon: LBMEmoticonManager = LBMEmoticonManager()
    
    lazy var packages = [LBMEmoticonPackage]()
    
    //私有化构造函数
    private override init() {
        super.init()
        loadAllEmoticons()
        
    }
    //加载分组表情数据
    func loadAllEmoticons() {
        
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        let dict = NSDictionary(contentsOfFile: path)!
        let packages = dict["packages"] as! [[String: AnyObject]]
        //遍历数组
        for item in packages {
            let id = item["id"] as! String
            //根据id加载分组表情文件夹
            loadSectionEmoticon(id)

            
        }

    }
    
    private func loadSectionEmoticon(id: String) {
       let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil,inDirectory: "Emoticons.bundle/" + id)!
        let dict = NSDictionary(contentsOfFile: path)!
        //获取分组的中文名称
        let name = dict["group_name_cn"] as! String
        //获取分组的表情数据
        let array = dict["emoticons"] as! [[String: String]]
        //通过表情分组模型转换
        let p = LBMEmoticonPackage(id: id, name: name, array: array)
        //添加到数组
        packages.append(p)
        
        
        
    }
    
    
    
}
